package com.zhxh.nzpos.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.zhxh.nzpos.db.DBHelper;
import com.zhxh.nzpos.model.Oprator;
import com.zhxh.nzpos.utils.RandomStringUtils;

/**
 * Oprator DAO
 * 
 * @author Administrator
 * 
 */
public class OpratorDao {

	DBHelper dbHelper;
	private final static String LOG_TAG = "OpratorDao";

	protected static final String TBL_NAME = "oprator";

	public OpratorDao(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * add new oprator
	 * 
	 * @param oprator
	 */
	public String save(Oprator oprator) {
	
		
		oprator.setId(RandomStringUtils.randomNumeric(10));
		long time = System.currentTimeMillis();
		oprator.setCreated(time);
		oprator.setModified(time);
		Log.i(LOG_TAG, "OpratorDao  save " + oprator.toString());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"insert into oprator(ID,MemberID,LimitsID,created,modified,password) values(?,?,?,?,?,?)",
					new Object[] { oprator.getId(), oprator.getMemberId(),
							oprator.getLimitsId(), oprator.getCreated(),
							oprator.getModified(),oprator.getPassword() });

			db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("OpratorDao  save", e.getMessage());
		}
		finally{
			db.endTransaction();// 事务结束;
			db.close();
		}

		return oprator.getId();
	}

	/**
	 * update a oprator
	 * 
	 * @param oprator
	 */

	public void update(Oprator oprator) {
		Log.i(LOG_TAG, "OpratorDao  update " + oprator.toString());
		oprator.setModified(System.currentTimeMillis());
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();// 开始事务
		try {
			db.execSQL(
					"update oprator set MemberID=?, LimitsID=?,modified=?,password=? where ID=?",
					new Object[] { oprator.getMemberId(),
							oprator.getLimitsId(), oprator.getModified(),oprator.getPassword(),
							oprator.getId() });

			// db.setTransactionSuccessful();
		} catch (Exception e) {
			Log.i("OpratorDao  update", e.getMessage());
		}
		finally{
			db.endTransaction();// 事务结束;
			db.close();
		}
	}

	/**
	 * get a oprator by id
	 * 
	 * @param i
	 * @return
	 */
	public Oprator find(String id) {
		Log.i(LOG_TAG, "OpratorDao  find " + id);
		Oprator oprator = null;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,MemberID,LimitsID,created,modified,password from oprator where ID=?",
						new String[] { id });
		if (cursor.moveToNext()) {
			oprator = new Oprator();

			oprator.setId(cursor.getString(cursor.getColumnIndex("ID")));
			oprator.setMemberId(cursor.getString(1));
			oprator.setLimitsId(cursor.getString(2));
			oprator.setCreated(cursor.getLong(3));
			oprator.setModified(cursor.getLong(4));
			oprator.setPassword(cursor.getString(5));
			
		}
		cursor.close();
		db.close();
		return oprator;
	}

	/**
	 * delete a oprator by id
	 * 
	 * @param id
	 */
	public void delete(String id) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from oprator where ID=?", new Object[] { id });
		db.close();
	}

	/**
	 * delete all
	 * 
	 * @param id
	 */
	public void deleteAll() {
		Log.i(LOG_TAG, "  delete all from  oprator: ");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.execSQL("delete from oprator");
		db.close();
	}

	/**
	 * fetch oprators
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public List<Oprator> getScrollData(int pageNumber, int pageSize) {
		List<Oprator> oprators = new ArrayList<Oprator>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db
				.rawQuery(
						"select ID,MemberID,LimitsID,created,modified,password from oprator order by modified desc limit ?,?",
						new String[] {
								String.valueOf((pageNumber - 1) * pageSize),
								String.valueOf(pageSize) });// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Oprator oprator = new Oprator();
			oprator.setId(cursor.getString(cursor.getColumnIndex("ID")));
			oprator.setMemberId(cursor.getString(1));
			oprator.setLimitsId(cursor.getString(2));
			oprator.setCreated(cursor.getLong(3));
			oprator.setModified(cursor.getLong(4));
			oprator.setPassword(cursor.getString(5));
			oprators.add(oprator);
		}
		cursor.close();
		db.close();
		return oprators;
	}

	/**
	 * fetch all oprators
	 * 
	 * @return
	 */
	public List<Oprator> getAll() {
		List<Oprator> oprators = new ArrayList<Oprator>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery(
				"select oprator.ID,oprator.MemberID,oprator.LimitsID,oprator.created,oprator.modified,oprator.password,member.CardID,member.Name,member.ShfzID,member.Phone from oprator,member order by oprator.modified desc",
				null);// pageNumber页码
		while (cursor.moveToNext()) {// pageSize每页获取的记录数
			Oprator oprator = new Oprator();
			oprator.setId(cursor.getString(cursor.getColumnIndex("ID")));
			oprator.setMemberId(cursor.getString(1));
			oprator.setLimitsId(cursor.getString(2));
			oprator.setCreated(cursor.getLong(3));
			oprator.setModified(cursor.getLong(4));
			oprator.setPassword(cursor.getString(5));
			oprator.setMemberId(cursor.getString(6));
			oprator.setName(cursor.getString(7));
			oprator.setShfzID(cursor.getString(8));
			oprator.setPhone(cursor.getString(9));
			oprators.add(oprator);
		}
		cursor.close();
		db.close();
		return oprators;
	}

	/**
	 * get oprator's count
	 * 
	 * @return
	 */
	public long getCount() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		long count = DatabaseUtils.queryNumEntries(db, TBL_NAME);
		db.close();
		return count;
	}

}
